import com.alibaba.fastjson.JSON;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author liurui
 * @date 2023/1/11 0:36
 */
public class App {
    public static void main(String[] args) {
        List<FeePO> feePOList = new ArrayList<>();
        feePOList.add(new FeePO(1, 4, 2, BigDecimal.ONE));
        feePOList.add(new FeePO(1, 6, 2, BigDecimal.ONE));
        feePOList.add(new FeePO(1, 7, 2, BigDecimal.ONE));
        feePOList.add(new FeePO(1, 1, 2, BigDecimal.TEN));
        feePOList.add(new FeePO(1, 1, 2, BigDecimal.TEN));
        feePOList.add(new FeePO(1, 10, 2, BigDecimal.TEN));
        feePOList.add(new FeePO(1, 7, 1, BigDecimal.ONE));
        feePOList.add(new FeePO(1, 1, 1, BigDecimal.TEN));
        feePOList.add(new FeePO(1, 1, 1, BigDecimal.TEN));
        feePOList.add(new FeePO(1, 10, 1, BigDecimal.TEN));
        feePOList.add(new FeePO(2, 4, 1, BigDecimal.ONE));
        feePOList.add(new FeePO(2, 6, 2, BigDecimal.ONE));
        feePOList.add(new FeePO(3, 4, 5, BigDecimal.ONE));
        feePOList.add(new FeePO(5, 9, 8, BigDecimal.ONE));
        feePOList.add(new FeePO(5, 4, 8, BigDecimal.ONE));
        feePOList.add(new FeePO(5, 4, 8, BigDecimal.ONE));
        feePOList.add(new FeePO(5, 4, 8, BigDecimal.ONE));

        List<ShopPO> shopPOList = new ArrayList<>();
        shopPOList.add(new ShopPO(1, "上海"));
        shopPOList.add(new ShopPO(2, "北京"));
        shopPOList.add(new ShopPO(3, "长沙"));
        shopPOList.add(new ShopPO(4, "天津"));

        Map<Integer, String> shopPOMap = shopPOList.stream().collect(Collectors.toMap(x -> x.getShopId(), y -> y.getShopName()));

        Map<Integer, List<FeePO>> map = feePOList.stream().collect(Collectors.groupingBy(e -> e.getType()));

        List<FeeAndTypeBO> feeAndTypeBOList = new ArrayList<>();
        map.forEach((k, v) -> {

            Map<Integer, List<FeePO>> shopMap = v.stream().collect(Collectors.groupingBy(e -> e.getShopId()));


            FeeAndTypeBO feeAndTypeBO = new FeeAndTypeBO();
            feeAndTypeBO.setType(k);

/*
            BigDecimal total = BigDecimal.ZERO;
            for (FeePO feePO : v) {
                total = total.add(feePO.getFee());

            }*/

            BigDecimal totalFee = v.stream().map(e -> e.getFee()).reduce(BigDecimal.ZERO, BigDecimal::add);

            feeAndTypeBO.setTotalFee(totalFee);

            List<ShopFeeBO> shopFeeBOList = new ArrayList<>();

            shopMap.forEach((k1, v1) -> {
                ShopFeeBO shopFeeBO = new ShopFeeBO();
                List<ShopFeeDetailBO> shopFeeDetailBOList = new ArrayList<>();
                shopFeeBO.setShopId(k1);
                BigDecimal fee = BigDecimal.ZERO;
                for (FeePO feePO : v1) {
                    fee = fee.add(feePO.getFee());

                    ShopFeeDetailBO bo = new ShopFeeDetailBO();
                    bo.setFee(feePO.getFee());
                    bo.setShopTypeId(feePO.getShopTypeId());
                    bo.setShopName(shopPOMap.get(k1));

                    shopFeeDetailBOList.add(bo);
                }
                shopFeeBO.setFee(fee);
                shopFeeBO.setShopFeeDetailBOList(shopFeeDetailBOList);
                shopFeeBOList.add(shopFeeBO);

            });

            feeAndTypeBO.setShopFeeBOList(shopFeeBOList);
            feeAndTypeBOList.add(feeAndTypeBO);

        });

        String s = JSON.toJSONString(feeAndTypeBOList);
        System.out.println(s);

    }
}
